package cn.walls1717.leetcode.array;

/**
 * 35.搜索插入位置
 * 力扣地址：https://leetcode-cn.com/problems/search-insert-position/
 *
 * @author walls1717
 * @since 2022-04-29
 **/
public class T35 {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 6};
        int target = 7;
        System.out.println(searchInsert(nums, target));
    }

    private static int searchInsert(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;
        int m;
        while (l <= r) {
            m = l + ((r - l) >> 1);
            if (nums[m] == target) {
                return m;
            } else if (nums[m] > target) {
                r = m - 1;
            } else {
                l = m + 1;
            }
        }
        return r + 1;
    }
}
